package FlowGraph.SSA;

import antlr.collections.AST;

import JavaGrammar.*;

//////////////////////////////////////////////////////////////////////////
//// PhiNode
/**

  Especifica a variavel, e qual e a definicao que gerou esse
phi node. A ultima posicao do labels e reservada para o indice da
definicao. O vetor booleano constant indica se a posicao do labels
deve ser tratada como uma constante ou indice. Isso e utilizado
durante o algoritmo de constant propagation.
    
@author Ivan Jeukens
@version v 0.1 11/02/2003
*/
public class PhiNode {
    
    public PhiNode(VariableInfo var, int inputSize) {
        _varInfo = var;
        _labels = new AST[inputSize + 1]; // O ultimo valor e o output
        _createAST();
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public  methods                    ////

    public VariableInfo getVariableInfo() {
        return _varInfo;
    }

    public void setLabel(int pos, int value) {
        String s = new String(_labels[pos].getText() + "_" + value);
        _labels[pos].setText(s);
    }

    public void setDefLabel(int value) {
        setLabel(_labels.length - 1, value);
    }

    public AST getLabel(int pos) {
        return _labels[pos];
    }

    public int inputSize() {
        return _labels.length - 1;
    }

    public AST getDefLabel() {
        return _labels[_labels.length - 1];
    }
    
    public AST getAST() {
        return _ast;
    }
    
    public String toString() {
        return new String("Phi node : " + _ast.toStringTree());
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    private void _createAST() {
        _ast = new LinkedAST();
        _ast.setType(JavaTokenTypes.EXPR);
        _ast.setText("EXPR");
        
        LinkedAST v = new LinkedAST();
        v.setType(JavaTokenTypes.ASSIGN);
        v.setText("=");

        _ast.setFirstChild(v);
        v.setParent(_ast);

        LinkedAST op;
        op = new LinkedAST();
        op.setType(JavaTokenTypes.IDENT);
        op.setText(_varInfo.getSTEntry().getName());

        _labels[_labels.length - 1] = op;

        v.setFirstChild(op);
        op.setParent(v);
        v = op;

        for(int i = 0;i < _labels.length - 1;i++) {
            op = new LinkedAST();
            op.setType(JavaTokenTypes.IDENT);
            op.setText(_varInfo.getSTEntry().getName());

            v.setNextSibling(op);
            op.setParent(v);

            _labels[i] = op;
            v = op;
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private AST [] _labels;
    private VariableInfo _varInfo;
    private LinkedAST _ast;
}
